InformIT
Exact Phrase
All Words
Enter search string and press enter!

Search Tips

MyInformIT - Collect Your Own Content Infobases - Premium Subscription Content AlphaBooks - Content in Progress Editors' Choice - What the Experts Read Free Library - Complete Books Online InformIT Store - Books and Software Download It - Freeware and Shareware ZD News - IT News and Views BrassRing.com Jobs - Find Your Dream Job

DPEC Online Training
Click to Recommend-IT
Top IT News from ZD Net
Shareware from Tucows
 
Cisco Knowledge SuiteLinux/Open SourceWindows NTWindows 2000Visual Basic
ConfigureITNetworkITWebITDatabaseITProgramIT
 

You are here : Home : Tutorial 2: Programming Beyond Visual Basic Using the Windows API

Tutorial 2: Programming Beyond Visual Basic
Using the Windows API

by Bob Reselman and Richard Peasley - June 12, 2000

Tutorial 2: Programming Beyond VB Using Windows API

Understanding the Windows API

 

Working with the API Viewer

 

Monitoring Mouse Movement with GetCursorPos

 

Keeping a Window Always on Top by Using SetWindowPos

 
Source  
 

Working with the API Viewer

Although you do notneed any additional tools in order to work with the Windows API , Visual Basic ships with a tool (the API Viewer) that allows you to cut and paste API functions, constants, and Windows-defined types right into your code. You access the API Viewer from the Visual Basic Start menu. Also, you can add the API Viewer to your VB IDE in the form of an Add-In, thus making it readily available to you while you work.

Simply put, the API allows you to access the DLLs in the Windows environment for programming purposes. Microsoft wisely put all reusable code into DLLs (dynamic link libraries) so that they can be used repeatedly. This not only saves valuable programming time, but also standardizes the programming community by allowing the same code to be used repeatedly. With the same code running in many similar applications, they become more compatible.

Add the API Viewer to the Add-In Menu

  1. Choose Add-In Manager from Visual Basic's Add-Ins menu.
  2. Select VB 6 API Viewer from the Available Add-Ins list.
  3. In the Load Behavior section in the lower right corner of the Add-In Manager dialog, select Load on Startup. (If this option is already checked, the API Viewer should already be available in the Add-Ins menu.)
  4. Click OK.
  5. Close Visual Basic and restart it.
  6. Choose API Viewer from the Add-Ins menu to open the API Viewer window.

In the following sections, you'll build a small program that shows you how to use some API functions to do things you normally can't do in VB. You will use the API Viewer to retrieve functions, constants, and Windows-defined types to help you accomplish this. You will also build a function that reports back the position of the cursor anywhere onscreen. To do this, you need to use the API function GetCursorPos.

To use the API Viewer to retrieve GetCursorPos, you need to configure the viewer for first-time use.

Configure the API Viewer

  1. Start the API Viewer.
  2. Choose Load Text File from the File menu (see Figure 1).

  3. Figure 1 You load API definition data into the API Viewer from either a text file or a database file.

  4. Select the file Win32API.txt from the Open File dialog.
  5. When the text file is loaded into the API Viewer (this takes about 15 seconds), choose Convert Text to Database from the File menu.
  6. Save the database file with the filename suggested by the API viewer, Win32API.mdb.

The API Viewer doesn't have API data definition built into it; you must load the data at the beginning of every session. After you configure the API Viewer to read its API definition data from a database file, you select the database file whenever you need to use the API Viewer. Although you can use the provided text file, Win32API.txt, using the database file makes the API Viewer perform faster.

After you configure the API Viewer, you can use it to select functions, constants, and Windows-defined types associated with the Windows API.

Select the API function GetCursorPos

  1. Select Declares from the API Type drop-down list (see Figure 2).
  2. Type GetCursorPos in the text box below the API Type drop-down list (See Figure 3). The API Viewer auto-searches input made in the search TextBox against items listed in the Available Items list. The API Viewer takes some time to do a search; if you type slowly, the API Viewer performs more accurately.
  3. When the GetCursorPos function is located in the Available Items list, click the Add button to add the function to the Selected Items list.
  4. From the API Type drop-down list, select Types.
  5. Type the word POINTAPI in the search text box.
  6. Click the Copy button to send the entire function and type definitions to the Clipboard (see Figure 4).
  7. Exit and start a new session of Visual Basic.
  8. Choose Add Module from Visual Basic's Project menu.
  9. Paste the function definition for GetCursorPos and the type definition for POINTAPI from the Clipboard to the General Declaration section of the module (see Figure 5).

Figure 2 You can locate constants, declares(API functions), and types in the API Viewer.

(1) Enables you to filter the available items in terms of constants, declares, or types

(2) Type the item you're looking for in the search text box.

(3) Displays items according to constants, declares, or types. You can scroll through the list, type a few letters in the text box above, or type directly into the list to do a search.

(4) Select whether you want a selected item to be Public or Private in scope

(5) When you find what you want, click to add the item to theSelected Items list

(6) Removes a selected item from the Selected Items list

(7) Clears the Selected Items list of all items

(8) Copies all items in the Selected Itemslist onto the Clipboard for pasting into your project

Figure 3 When you enter a word in the search text box, the API Viewer looks through the available items on a letter-by-letter basis.

Figure 4 The Selected Items list holds selected constants, declares, and types that you can copy to the Clipboard in one process.

Figure 5 You must copy API declarations into the General Declarations section of a module.

Notice that GetCursorPos takes a Windows-defined type, POINTAPI, as an argument. You need to include the type definition in your VB project for the code to work.

 

Previous | Next

 

What's New in Visual Basic

Moving Components from MTS to COM+

Visual Basic and OOP: The Next Generation

Creating E-Mail Applications

Featured Author

Havoc Pennington Havoc Pennington

Havoc Pennington works for Red Hat Advanced Development Labs, where he enhances the Linux desktop development infrastructure and user experience. He is the author of several components of the GNOME GUI libraries, and is the primary architect of Gconf, a new GNOME configuration database library.

Featured Book

Sams Teach Yourself Shell Programming in 24 Hours Sams Teach Yourself Shell Programming in 24 Hours

Take control of your systems by harnessing the power of the shell.

 
 
 

About InformIT

|

Press Area

|

Advertising

|

Careers

Contact Us

|

Copyright, Terms & Conditions

|

Privacy Policy

 

© Copyright 2000 InformIT. All rights reserved.